<?php
namespace app\web\model;

use app\lib\model\Price;


/**
 * @package app\basic\model
 */
class Comment extends Base 
{
    
    protected function getCreateTimeAttr($v){
        return date('Y-m-d H:i',$v);
    }
    protected function getCreateTime1Attr($v){
        return date('Y-m-d',$v);
    }
    protected function setImagesAttr($v){
        return $v ? (is_array($v) ? implode(',', $v) : $v) : ''; 
    }
    protected function getImagesAttr($v){
        return $v ? explode(',', $v) : [];
    }
    /**
     * 修改订单好评率
     * [shopPraiseRate description]
     * @param  [type] $shop_id [description]
     * @return [type]          [description]
     */
    public static function shopPraiseRate($shop_id)
    {
        if(!$shop_id) return true;
        $tot = self::where(['shop_id'=>$shop_id])->count();
        if(!$tot) return true;
        $hao_tot = self::where(['shop_id'=>$shop_id,'is_star'=>2])->count();
        $shop = Shop::get($shop_id);
        $shop->praise_rate = Price::pricecalc(100,'*',Price::pricecalc($hao_tot,'/',$tot),0);
        $res = $shop->save();
        return $res ? true : false;
    }
    /**
     * 每次评论计算技师评分
     * [technicianScore description]
     * @param  [type] $t_id [description]
     * @return [type]       [description]
     */
    public static function technicianScore($t_id)
    {
        $data = self::where(['technician_id'=>$t_id])->column('star');
        $score = Price::pricecalc(array_sum($data),'/',count($data));
        $technician = Technician::get($t_id);
        $technician->score = $score;
        $res = $technician->save();
        return $res ? true : false;
    }
}
